﻿using PwjCy.Lite.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PwjCy.Lite.EntityLite.Imp
{
    /// <summary>
    /// DAO基类对象
    /// </summary>
    public class PwjCyObject : IPwjCyObject
    {
        private DateTime _CreatedTime { get; set; }
        private DateTime _ModifyTime { get; set; }
        private ObjectOperateType _ObjectOperateType { get; set; }
        private ObjectTranscopeType _ObjectTranscopeType { get; set; }
        private object _Id { get; set; }
        private string _TableName { get; set; }
        private string _StorageAlias { get; set; }
        private IList<IPwjCyObject> _NeedPwjCyObjectList { get; set; }
        private Type _DeclareType { get; set; }
        public PwjCyObject()
        {
            _DeclareType = this.GetType();
            _CreatedTime = DateTime.Now;
            _ModifyTime = DateTime.Now;
            _ObjectOperateType = ObjectOperateType.Create;
            _ObjectTranscopeType = ObjectTranscopeType.None;
            _Id = LiteCoreHelper.GetGuid();
            _TableName = this.GetType().FullName.Substring(_DeclareType.FullName.LastIndexOf('.') + 1);
            _StorageAlias = "Current";
        }
        public void Remove()
        {
            _ObjectOperateType = ObjectOperateType.Delete;
        }
        internal void GetPwjCyObject(object Id)
        {
            this._ObjectOperateType = ObjectOperateType.Update;
            this._Id = Id;
            this._ModifyTime = DateTime.Now;
        }
        internal void SetStorageAlias(string storageAlias)
        {
            if (string.IsNullOrWhiteSpace(storageAlias)) return;
            this.StorageAlias = storageAlias;
        }

        public DateTime CreatedTime
        {
            get { return _CreatedTime; }
            internal set { _CreatedTime = value; }
        }
        public DateTime ModifyTime
        {
            get { return _ModifyTime; }
            internal set { _ModifyTime = value; }
        }
        [IgnoreDataTypeAttribute]
        public ObjectOperateType ObjectOperateType
        {
            get
            {
                return _ObjectOperateType;
            }
        }
        [IgnoreDataTypeAttribute]
        public ObjectTranscopeType ObjectTranscopeType
        {
            get
            {
                return _ObjectTranscopeType;
            }
        }
        public object Id
        {
            get
            {
                return _Id;
            }
            set
            {
                if (ObjectOperateType != ObjectOperateType.Create) { _Id = value; }
            }
        }
        [IgnoreDataTypeAttribute]
        public string StringId { get { return _Id.ToString(); } }
        public int Version { get; set; }
        [IgnoreDataTypeAttribute]
        public string TableName
        {
            get { return _TableName; }
            set { _TableName = value; }
        }
        [IgnoreDataTypeAttribute]
        public string StorageAlias
        {
            get { return _StorageAlias; }
            set { _StorageAlias = value; }
        }
        /// <summary>
        /// 添加DAO对象
        /// </summary>
        /// <param name="pwjcyObject"></param>
        public void AddNeedPwjCyObject(IPwjCyObject pwjcyObject)
        {
            //优先级高的可以覆盖低的
            if (((int)pwjcyObject.ObjectTranscopeType) > ((int)this.ObjectTranscopeType))
            {
                this._ObjectTranscopeType = pwjcyObject.ObjectTranscopeType;
            }
            _NeedPwjCyObjectList.Add(pwjcyObject);
        }
        private ObjectTranscopeType GetPwjCyObjectInitObjectTranscopeType()
        {
            //按优先级来 分库优先级最高，接着分表，最后单库
            //1.是否有分库分表
            if (_DeclareType.GetInterface("IDataRadiationClassifier", false) != null)
            {
                //判断是否有分库,无则返回分表事务

                return ObjectTranscopeType.DbTransaction;
            }
            //2.无分库分表
            return ObjectTranscopeType.None;
        }
        protected IEnumerable<IPwjCyObject> NeedPwjCyObject { get { return _NeedPwjCyObjectList.ToArray(); } }
    }
}
